package org.odk.clinic.adroid.service;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.odk.clinic.android.openmrs.Constants;

public class Obs {
	
	public ArrayList<String> GetAllObservationByIDPatient(int idpatient){
		ArrayList<String> observation = new ArrayList<String>();
		int NumCount;
		
		//Dictionary<String, String> obser = null;
		String ServicePage = "GetAllObsByIDPatient.asmx";
		String METHOD_NAME = "GetallobsByidpatient";
		SoapObject responseBody = null; //Content file XML
		SoapObject table = null; // Table hold data get back form service
		SoapObject tableRow = null; // Row of table
		
		SoapObject Request = new SoapObject(Constants.NAMESPACE, METHOD_NAME);
		Request.addProperty("idpatient", idpatient);
		SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelop.dotNet = true;
		envelop.encodingStyle = SoapSerializationEnvelope.ENC;
		envelop.setOutputSoapObject(Request);
		AndroidHttpTransport transport = new AndroidHttpTransport(Constants.URL + ServicePage);
		try{
			transport.call(Constants.SOAP_ACTION + METHOD_NAME, envelop);
			SoapObject result = (SoapObject)envelop.bodyIn;
			try{
				if(result.getPropertyCount() > 0 )
				{
					//Get all file XML
					responseBody = (SoapObject)envelop.getResponse();
					//Reject information XML and get back result
					responseBody = (SoapObject)responseBody.getProperty(1);
					//Get information of back table
					table = (SoapObject)responseBody.getProperty(0);
					//Count records in file XML
					NumCount = table.getPropertyCount();
					for(int i = 0; i < NumCount; i++)
					{
						tableRow = (SoapObject)table.getProperty(i);
						observation.add(tableRow.getProperty("name").toString() +
								"@" + tableRow.getProperty("concept_id").toString() +
								"@" + tableRow.getProperty("date_created").toString() +
								"@" + tableRow.getProperty("value_numeric").toString());
						
					}
				}
				return observation;
			}catch(Exception ex){
				ex.printStackTrace();
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public ArrayList<String> GetObsbyIDPatientAndIDConcept(int idpatient, int idconcept){
		ArrayList<String> concept = new ArrayList<String>();
		int Num;
		
		String ServicePage = "GetAllObsByIDPatient.asmx";
		String METHOD_NAME = "GetObsByIDPatientAndIDConcept";
		SoapObject responseBody = null; //Content file XML
		SoapObject table = null; // Table hold data get back form service
		SoapObject tableRow = null; // Row of table
		
		SoapObject Request = new SoapObject(Constants.NAMESPACE, METHOD_NAME);
		Request.addProperty("idpatient", idpatient);
		Request.addProperty("idconcept", idconcept);
		SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelop.dotNet = true;
		envelop.encodingStyle = SoapSerializationEnvelope.ENC;
		envelop.setOutputSoapObject(Request);
		AndroidHttpTransport transport = new AndroidHttpTransport(Constants.URL + ServicePage);
		try{
			transport.call(Constants.SOAP_ACTION + METHOD_NAME, envelop);
			SoapObject result = (SoapObject)envelop.bodyIn;
			try{
				if(result.getPropertyCount() > 0 )
				{
					//Get all file XML
					responseBody = (SoapObject)envelop.getResponse();
					//Reject information XML and get back result
					responseBody = (SoapObject)responseBody.getProperty(1);
					//Get information of back table
					table = (SoapObject)responseBody.getProperty(0);
					//Count records in file XML
					Num = table.getPropertyCount();
					for(int i = 0; i < Num; i++)
					{
						tableRow = (SoapObject)table.getProperty(i);
						concept.add(tableRow.getProperty("name").toString() +
								"@" + tableRow.getProperty("date_created").toString() +
								"@" + tableRow.getProperty("obs_datetime").toString() +
								"@" + tableRow.getProperty("value_numeric").toString());
						
					}
				}
				return concept;
			}catch(Exception ex){
				ex.printStackTrace();
				return null;
			}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}
